package com.xc.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.common.Result;
import com.xc.demo.entity.Role;
import com.xc.demo.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping
    public Result findall(){

        return Result.success(roleService.list());

    }


    //    新增数据
    @PostMapping
    public boolean save(@RequestBody Role role){
        return roleService.saveOrUpdate(role);
    }


//    删除数据
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return roleService.removeById(id);
    }


//    查询展示分页
    @GetMapping("/page")
    public IPage<Role> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String uname
    ){
        log.info("pageNum={},pageSize={},username={}",pageNum,pageSize,uname);

        IPage<Role> page= new Page<>(pageNum,pageSize);

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        if(!"".equals(uname)){
            queryWrapper.like("name",uname);
        }

        queryWrapper.orderByAsc("id");

        return roleService.page(page,queryWrapper);
    }


//    绑定角色和菜单的关系 角色id和菜单id
      @PostMapping("/roleMenu/{roleId}")
     public Result roleMenu(@PathVariable Integer roleId , @RequestBody List<Integer> menuIds) {
        roleService.setRoleMenu(roleId,menuIds);
        return Result.success();

    }


    @GetMapping("/roleMenu/{roleId}")
    public Result GetRoleMenu(@PathVariable Integer roleId) {

        return Result.success(roleService.getRoleMenu(roleId));

    }


}
